package worker

import (
	"NovelBook/dao"
	"NovelBook/models/db_models"
	"NovelBook/models/res"
	"gorm.io/gorm"
)

func GetWorkers() ([]res.UserInfo, error) {
	var workers []res.UserInfo
	if err := dao.GetDb().Model(db_models.Worker{}).Where("id > ?", 1).Find(&workers).Error; err != nil {
		return nil, err
	}
	return workers, nil
}

func GetWorkersOn() ([]res.UserInfo, error) {
	var workers []res.UserInfo
	if err := dao.GetDb().Model(db_models.Worker{}).Where("id > ? AND state = ?", 1, 1).Find(&workers).Error; err != nil {
		return nil, err
	}
	return workers, nil
}

func GetWorkersOff() ([]res.UserInfo, error) {
	var workers []res.UserInfo
	if err := dao.GetDb().Model(db_models.Worker{}).Where("id > ? AND state != ?", 1, 1).Find(&workers).Error; err != nil {
		return nil, err
	}
	return workers, nil
}

func GetWorkersByID(WorkerIDs []string) ([]res.UserInfo, error) {
	var workers []res.UserInfo

	for _, ID := range WorkerIDs {
		var worker res.UserInfo
		if err := dao.GetDb().Model(db_models.Worker{}).Where("id = ?", ID).First(&worker).Error; err != nil {
			if err == gorm.ErrRecordNotFound {
				var zero res.UserInfo
				workers = append(workers, zero)
			} else {
				return nil, err
			}
		} else {
			workers = append(workers, worker)
		}
	}
	return workers, nil
}
